<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="/static/admin/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/admin/layuimini/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn layui-btn-normal" id="btn-add"> 添加 </button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="/static/admin/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script src="/static/admin/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'parentid',
            elem: '#munu-table',
            url: "{:url('menu/index',['_action'=>'getMenuJson'])}",
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'name', minWidth: 200, title: '菜单名称'},
                {field: 'listorder', minWidth: 200, align: 'center', title: '排序号'},
                {
                    field: 'type', minWidth: 200, align: 'center', templet: function (d) {
                        if (d.type == 1) {
                            return '<span class="layui-badge layui-bg-blue">是</span>';
                        } else {
                            return '<span class="layui-badge-rim">否</span>';
                        }
                    }, title: '权限菜单'
                },
                {
                    field: 'status', width: 80, align: 'center', templet: function (d) {
                        if (d.status == 1) {
                            return '<span class="layui-badge layui-bg-blue">显示</span>';
                        } else {
                            return '<span class="layui-badge-rim">不显示</span>';
                        }
                    }, title: '状态'
                },
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
        
        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
            console.log(insTb);
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $('#btn-add').click(function () {
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['80%', '80%'],
                content: "{:url('menu/add')}",
                end:function(){
                    location.reload();
                }
            });
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                $.ajax({
                    url: "{:url('menu/delete')}",
                    type: 'post',
                    dataType: 'json',
                    data:{id:data.id},
                    success: function (data) {
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function(){
                                if(data.status){
                                   obj.del();
                                }
                            });
                    },
                    error: function (xhr, textstatus, thrown) {
                        return layer.msg('Status:' + xhr.status + '，' + xhr.statusText + '，请稍后再试！');
                    }
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '编辑菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: "{:url('menu/edit')}"+'?id='+data.id,
                    end:function(){
                        location.reload();
                    }
                });
            }
        });
    });
</script>
</body>
</html>